package com.ziyun.erp.web.modules.material.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.modules.material.entity.MaterialCategoryEntity;
import com.ziyun.erp.modules.material.service.MaterialCategoryService;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 耗材分类表
 *
 * @author guizhao
 * @email ...
 * @date 2018-08-08 11:34:35
 */
@RestController
@RequestMapping("material/materialcategory")
public class MaterialCategoryController {
    @Autowired
    private MaterialCategoryService materialCategoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("material:materialcategory:list")
    public List<MaterialCategoryEntity> list(){
        List<MaterialCategoryEntity> materialCategoryList = materialCategoryService.selectList(
                new EntityWrapper<MaterialCategoryEntity>().eq("`delete`",0)
                        //按指定排序
                .orderBy("category_order",true)
        );
        for(MaterialCategoryEntity materialCategoryEntity : materialCategoryList){
            MaterialCategoryEntity parentMaterialCategoryEntity = materialCategoryService.selectById(materialCategoryEntity.getParentId());
            if(parentMaterialCategoryEntity != null){
                materialCategoryEntity.setParentName(parentMaterialCategoryEntity.getCategoryName());
            }
        }

        return materialCategoryList;
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
    @RequiresPermissions("material:materialcategory:info")
    public R info(@PathVariable("categoryId") Integer categoryId){
        MaterialCategoryEntity materialCategory = materialCategoryService.selectById(categoryId);

        return R.ok().put("materialCategory", materialCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("material:materialcategory:save")
    public R save(@RequestBody MaterialCategoryEntity materialCategory){
        // 获取当前用户id
        Long userId = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUserId();
        materialCategory.setCreateName(""+userId);

        String parentName = materialCategory.getParentName();
        Integer getlevel = materialCategoryService.getlevel(parentName);
        if(getlevel == null){
            getlevel = 1;
        }
        if(getlevel>=2){
            return R.error("请选择二级分类");
        }
        if (materialCategory.getParentId() > 0){ // 不是一级分类则加上分类路径
            MaterialCategoryEntity parent = materialCategoryService.selectById(materialCategory.getParentId());
            materialCategory.setCategoryPath(parent.getCategoryPath());
            materialCategory.setLevel((getlevel+1));
            materialCategory.setCategoryPathName(parent.getCategoryPathName()+","+materialCategory.getCategoryName());
        } else {
            materialCategory.setLevel(getlevel);
            materialCategory.setCategoryPathName(","+materialCategory.getCategoryName());
        }
        materialCategoryService.insertMaterialCategory(materialCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("material:materialcategory:update")
    public R update(@RequestBody MaterialCategoryEntity materialCategory){
        ValidatorUtils.validateEntity(materialCategory);
        materialCategoryService.updateAllColumnById(materialCategory);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("material:materialcategory:delete")
    public R delete(Integer categoryId){
        //判断是否有子菜单或按钮
        List<MaterialCategoryEntity> materialCategoryList = materialCategoryService.queryListParentId(categoryId);
        if(materialCategoryList.size() > 0){
            return R.error("请先删除子分类");
        }
        materialCategoryService.deleteUpdate(categoryId);

        return R.ok();
    }

    /**
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/select")
    @RequiresPermissions("material:materialcategory:select")
    public R select(){
        //查询列表数据
        List<MaterialCategoryEntity> materialCategoryList = materialCategoryService.queryAllList();

        //添加顶级菜单
        MaterialCategoryEntity root = new MaterialCategoryEntity();
        root.setCategoryId(0);
        root.setCategoryName("一级分类");
        root.setParentId(-1);
        root.setOpen(true);
        materialCategoryList.add(root);

        return R.ok().put("materialCategoryList", materialCategoryList);
    }

    /**
     * 获取分类树信息
     */
    @RequestMapping("/getTree")
    public R getTree(){
        return  materialCategoryService.getTree();
    }
}
